
package com.hsnn.medstgmini.base.dict.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.service.DicDrugformLogManager;
import com.hsnn.medstgmini.base.dict.service.DicDrugformManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * 基础库 数据字典 剂型字典 控制层
 * @author hss@njhsnn
 * 2015年6月9日
 */
@Controller
@RequestMapping("/dicDrugformLog")
public class DicDrugformLogController {
	
	private static final Logger log = Logger.getLogger(DicDrugformLogController.class);
	
	@Autowired
	private HttpServletRequest request;

	@Source
	private DicDrugformLogManager dicDrugformLogManager;

	@Source
	private DicDrugformManager dicDrugformManager;

	@Autowired
	public void setDicDrugformLogManager(DicDrugformLogManager dicDrugformLogManager) {
		this.dicDrugformLogManager = dicDrugformLogManager;
	}

	@Autowired
	public void setDicDrugformManager(DicDrugformManager dicDrugformManager) {
		this.dicDrugformManager = dicDrugformManager;
	}

	/**
	 * 基础库 数据字典 剂型字典 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/log")
	public String dicDrugformLogDetail(HttpServletRequest request,HttpServletResponse response) throws IOException {
		printParams();
		String formId = request.getParameter("formId");
		String formName = new String(request.getParameter("formName").getBytes("ISO8859-1"),"utf-8");
		request.setAttribute("formName", formName);
		request.setAttribute("formId", formId);
		request.setAttribute("formCode", request.getParameter("formCode"));
		return "/base/dict/drugFormHis";// 跳转到详情页面
	}

	
	/**
	 * 基础库 数据字典 剂型字典 查询及条件查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/dicDrugformLogList")
	@ResponseBody
	public Pagination getDicDrugformLogList(HttpServletRequest request) {
		printParams();
		Pagination page = new Pagination(request);
		try {
			String date1 = request.getParameter("date1");
			String date2 = request.getParameter("date2");
			String formId = request.getParameter("formId");
			String formCode = request.getParameter("formCode");
			if(StringUtils.isNotBlank(date1)){
				page.getConditions().put("date1",date1.trim());
			}
			if(StringUtils.isNotBlank(date2)){
				page.getConditions().put("date2",date2.trim());
			}
			page.getConditions().put("formId",formId.trim());
			if(StringUtils.isNotBlank(formCode)){
				page.getConditions().put("formCode",formCode.trim());
			}
			Pagination data = dicDrugformLogManager.queryDicDrugformLogList(page);
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} catch (Error error) {
			error.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 基础库 数据字典 剂型字典 根据id查询剂型 日志详情
	 * @param result
	 * @param dicDrugformLog
	 * @return
	 */
	@RequestMapping("/toDicDrugformLogDetail")
	public String toDicDrugformLogDetail(){
		printParams();
		Pagination  page = new Pagination();
		page.getConditions().put("logId", request.getParameter("logId"));
		List<Map<String,Object>> list = dicDrugformLogManager.getDicDrugformLogDetail(page);
		request.setAttribute("list", list);
		return "/base/dict/drugformLogDetail";
		
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author 徐国鹏
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
	
	
	
	
}
